package com.hrt.web.shoppingCar.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.hrt.base.BaseController;
import com.hrt.bean.ShoppingCar;
import com.hrt.web.shoppingCar.service.ShoppingCarService;
@Controller
@RequestMapping("web/shoppingCar")
public class ShoppingCarController extends BaseController<ShoppingCar>{
	/**礼品基本信息*/
	@Autowired
	private ShoppingCarService shoppingCarService;
	/**
	 * 
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/list")
	public ModelAndView list(@RequestParam(value = "memberId",required = true) Long memberId,
			HttpServletRequest request) {
		ModelAndView view = new ModelAndView();
		
		List<ShoppingCar> carList = shoppingCarService.getShoppingCarByMemberId(memberId);
		view.addObject("carList", carList);
		view.addObject("memberId", memberId);
		view.setViewName("/HRTWEB/shoppingCar/shoppingCar");
		return view;

	}
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/listCar")
	@ResponseBody
	public Map listCar(
			@RequestParam(value = "memberId",required = true) Long memberId,
			HttpServletRequest request) {
		Map resultMap = new HashMap();
		List<ShoppingCar> carList = shoppingCarService.getShoppingCarByMemberId(memberId);
		resultMap.put("carList", carList);
		return resultMap;
	}
	@RequestMapping(value = "/changeShoppingCar")
	@ResponseBody
	public Map changeShoppingCar(
			@RequestParam(value = "carId",required = true) Long carId,
			@RequestParam(value = "total",required = false) Integer total,
			HttpServletRequest request) {
		Map resultMap = new HashMap();
		ShoppingCar car = shoppingCarService.getCarById(carId);
		car.setTotal(total);
		car.setSummery(total*car.getSalePrice());
		shoppingCarService.updateCar(car);
		resultMap.put("car", car);
		return resultMap;
	}
	
	/**
	 * 单独发货
	 * @param orderId
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/delItemById")
	@ResponseBody
	public String delItemById(
			@RequestParam(value = "id", required = true) Long carId,
			HttpServletRequest request, HttpServletResponse response) {
		if (carId != null && !"".equals(carId)) {
			shoppingCarService.deleteByCarId(carId);
			return "success";
		}
		return "fail";
		
	}
}
